package com.promis.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class CollectionUtils {

	public static boolean isNullOrEmpty(Map<?, ?> map) {
		return map == null || map.isEmpty();
	}

	public static boolean isNullOrEmpty(Collection<?> coll) {
		return null == coll || coll.isEmpty();
	}

	public static <T> boolean isNullOrEmpty(T[] objs) {
		return objs == null || objs.length <= 0;
	}

	public static <T> boolean contains(T[] objs, T obj) {
		boolean isContains = false;
		if (isNullOrEmpty(objs) || obj == null)
			return isContains;
		for (T o : objs) {
			if (obj.equals(o)) {
				isContains = true;
				break;
			}
		}
		return isContains;
	}
	
	public static <T> T[] toArray(Collection<T> coll) {
		if (!CollectionUtils.isNullOrEmpty(coll)) {
			T[] ts = null;
			for (T t : coll) {
				ts = (T[]) Array.newInstance(t.getClass(), coll.size());
				break;
			}
			return coll.toArray(ts);
		} else return null;
	}

	public static <T> List<T> toList(T... ts) {
		if (!CollectionUtils.isNullOrEmpty(ts)) {
			List list = new ArrayList(ts.length);
			for (T t : ts) {
				list.add(t);
			}
			return list;
		} else return new ArrayList();
	}

	public static <T> void arrayAddToList(List list, T... ts) {
		if (!CollectionUtils.isNullOrEmpty(ts)) {
			for (T t : ts) {
				list.add(t);
			}
		}
	}
}
